package readWriteFieldsRelations;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Collection;
import model.TestClusterBuilder;
import util.GAConfig;
import util.Log;
import model.StatementKind;
import model.Filter;
import util.CanBeUsedAs;
public class MethodDependences {

	private final MethodRelations relations;

	public MethodDependences(Collection<Class<?>> classesToTest) {
		relations = new MethodRelations(classesToTest);
		//this.buildDependence();
	}

	public void buildDependence(List<StatementKind> models) {
		try {
			relations.buildRelations(models);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public MethodRelations getRelations() {
		return this.relations;
	}

	public String showDependence(Set<Class<?>> classToTest) {
		return this.relations.showDependence( classToTest);
	}

	public String showStatmentsWriteFileds(Set<Class<?>> classToTest) {
		return this.relations.showStatmentsWriteFileds(classToTest);
	}

	public static void main(String[] args) throws ClassNotFoundException {
		String logFilePath = "C:/MAMDA/MAMDA2014/temp/MethodDepenedences.txt";
		Log.logConfig(logFilePath);
		new File(logFilePath).getAbsolutePath();
		Log.log("CUT is:" + GAConfig.classFilePath);

		System.out.println("CUT is:" + GAConfig.classFilePath);
		Set<Class<?>> allClasses = new LinkedHashSet<Class<?>>();
		//get class from file
		allClasses.addAll(TestClusterBuilder.readClassFromFile());
		TestClusterBuilder TestCluster = new TestClusterBuilder(allClasses);
		System.out.println("There are " + allClasses.size()
				+ " classes as input");
		Log.log("There are " + allClasses.size() + " classes as input");
		
	      System.out.println("There are " + TestCluster.getClassToTest().size() + " classes to test after filtering");
			Log.log("There are " + TestCluster.getClassToTest().size() + " classes to test after filtering");
			Set<Class<?>> classToTest= new LinkedHashSet<Class<?>>();

			for(Class<?> cls: TestCluster.getClassToTest().values())
				classToTest.add(cls);
	
			Set<Class<?>> cluster = new LinkedHashSet<Class<?>>();
		
			 for(String cName:TestCluster.getCluster().keySet()){
					Class<?> cls=TestCluster.getCluster().get(cName).getClazz();
					if(Filter.isPrimitiveOrStringType(cls))continue;
					if(cls.getName().contains("java")) continue;
					if(cls.getName().startsWith("sun")) continue;
					if(cls.getName().contains("$")) continue;
					if(!Filter.canUse(cls))continue;
					cluster.add(cls);
				}
	    System.out.println("custer classes:"+ cluster);
		MethodDependences recommender = new MethodDependences(cluster);

		recommender.buildDependence(TestCluster.getStatements());
		System.out.println(recommender.relations.showFieldReadWrites(classToTest));
		Log.log(recommender.relations.showFieldReadWrites(classToTest));

		System.out.println(recommender.showDependence(classToTest));
		Log.log(recommender.showDependence(classToTest));
		System.out.println(recommender.showStatmentsWriteFileds(classToTest));
		Log.log(recommender.showStatmentsWriteFileds(classToTest) + "\n");

		System.out.println();
		System.exit(0);
	}

}
